package view.admin.factoryManage;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXTextField;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.user.UserEntity;
import pojo.user.UserProperty;
import view.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class FactoryManageController {

    private Main myApp;
    private static List<UserEntity> userEntities = new ArrayList<>() ;
    public FactoryManageController() {
    }
    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    @FXML
    private TableView<UserProperty> factoryTable;

    @FXML
    private TableColumn<UserProperty, Void> option;

    @FXML
    private TableColumn<UserProperty,String> id;

    @FXML
    private TableColumn<UserProperty,String> factoryName;

    @FXML
    private TableColumn<UserProperty,String> factoryDetail;

    @FXML
    private TableColumn<UserProperty,String> name;

    @FXML
    private TableColumn<UserProperty,String> phone;

    @FXML
    private TableColumn<UserProperty,String> account;

    @FXML
    private TableColumn<UserProperty,String> state;

    @FXML
    private JFXTextField searchFactoryName;

    @FXML
    void initialize() throws IOException {
        UserDao userDao = new UserDao();
        this.displayInfo(userDao.findUserByPower("云工厂"));
    }

    @FXML
    void changeState(ActionEvent event) throws IOException {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        UserDao userDao = new UserDao();
        if(userEntities.size() == 0){
            alert.setHeaderText("!");
            alert.setContentText("至少选择一个");
            alert.showAndWait();
            userEntities = new ArrayList<>();
        } else {
            for(UserEntity user : userEntities) {
                userDao.changeState(user);
            }
        }
        this.displayInfo(userDao.findUserByPower("云工厂"));

    }

    @FXML
     void returnAllInfo(ActionEvent event) throws IOException {
        UserDao userDao = new UserDao();
        this.displayInfo(userDao.findUserByPower("云工厂"));

    }

    @FXML
    void searchFactory(ActionEvent event) throws IOException {
        UserDao userDao = new UserDao();
        String str =  searchFactoryName.getText();
        List<UserEntity> users = new ArrayList<>();
        users.add(userDao.searchFactoryName(str));
        this.displayInfo(users);
    }

    @FXML
    void returnMenu(ActionEvent event) throws Exception {
        myApp.goToAdminLoginPage();
    }

    private void addButtonToTable(){
        UserDao userDao = new UserDao();
        Callback<TableColumn<UserProperty, Void>, TableCell<UserProperty, Void>> cellFactory = new Callback<TableColumn<UserProperty, Void>, TableCell<UserProperty, Void>>() {
            @Override
            public TableCell<UserProperty, Void> call(final TableColumn<UserProperty, Void> param) {
                final TableCell<UserProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            UserProperty userProperty = getTableView().getItems().get(getIndex());
                            try {
                                userEntities.add(userDao.findUser(userProperty.getAccount()));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        option.setCellFactory(cellFactory);

    }
    void displayInfo(List<UserEntity> userEntityList){
        factoryTable.getItems();
        ObservableList<UserProperty> users = FXCollections.observableArrayList();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){

                UserProperty userProperty = new UserProperty();
                userProperty.setAccount(user.getAccount());
                userProperty.setId(user.getId());
                userProperty.setName(user.getName());
                userProperty.setPhone(user.getPhone());
                userProperty.setDetail(user.getDetail());
                userProperty.setFactoryName(user.getFactoryName());
                userProperty.setState(user.getState());
                users.add(userProperty);
            }
            addButtonToTable();
            id.setCellValueFactory(param -> param.getValue().idProperty());
            account.setCellValueFactory(param -> param.getValue().accountProperty());
            name.setCellValueFactory(param -> param.getValue().nameProperty());
            phone.setCellValueFactory(param -> param.getValue().phoneProperty());
            factoryName.setCellValueFactory(param -> param.getValue().factoryNameProperty());
            factoryDetail.setCellValueFactory(param -> param.getValue().detailProperty());
            state.setCellValueFactory(param -> param.getValue().stateProperty());

            factoryTable.setItems(users);
        }else{
            factoryTable.setItems(FXCollections.emptyObservableList());
        }

    }
}
